package com.jingtai.platform.page;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jingtai.platform.service.admin.PlatformAccountService;
import com.jingtai.platform.util.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/platform/page")
public class PageController {

    @Autowired
    private PlatformAccountService platformAccountService;

    @ApiOperation("进入首页")
    @GetMapping("/")
    public String showIndex(){
        return "/shop/index";
    }

    @ApiOperation("进入登录页")
    @GetMapping("/login")
    public String showLogin() {
        return "/shop/login";
    }

    @ApiOperation("进入公共页")
    @GetMapping("/include")
    public String showInclude() {
        return "/shop/include";
    }

    @ApiOperation("进入指定页")
    @GetMapping("/{page1}/edit")
    public String showpecified(@PathVariable("page1") String page1,String key, Long id, String url, Model model, HttpServletRequest request) {
        JSONObject jsonObject = sendGet("http://" + request.getLocalName() + ":" + request.getLocalPort() + url,id);
        model.addAttribute(key,jsonObject.toJavaObject(ResultUtil.class).getData());
        return "/shop/" + page1 + "/edit";
    }

    @ApiOperation("进入指定页")
    @GetMapping("/{page1}/{page2}")
    public String showpecified(@PathVariable("page1") String page1, @PathVariable("page2")  String page2,String type,String id,String applyState,String goodsCode,String supplyId,String merchantsId,Model model) throws Exception {
        model.addAttribute("type",type);
        model.addAttribute("applyState",applyState);
        model.addAttribute("id",id);
        model.addAttribute("goodsCode",goodsCode);
        model.addAttribute("supplyId",supplyId);
        model.addAttribute("merchantsId",merchantsId);
        return "/shop/" + page1 + "/" + page2;
    }

    /**
     *
     * get请求
     *
     * 银行接口数据获取
     */
    public JSONObject sendGet(String url,Long id){
        //创建连接对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        //post请求
        HttpGet httpGet = new HttpGet();
        try {
            //转换
            httpGet.setURI(URI.create(url +"?id="+ id));
            //发送请求
            HttpResponse response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                //成功后响应数据
                String result = EntityUtils.toString(response.getEntity());
                JSONObject json = JSON.parseObject(result);
                System.out.println("接口返回数据："+json.toString());
                return json;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                //释放连接
                if(httpClient != null){
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new JSONObject();
    }



    @ApiOperation("平台账户登出")
    @GetMapping("/platformLogOut")
    @ResponseBody
    public ResultUtil platformLogOut(HttpServletRequest request) {
        return platformAccountService.platformLogOut(request);
    }


}
